package com.xtrip.adm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tcl.platform.database.query.SimpleQuery;
import com.xtrip.adm.stat.data.TcHotelList;
import com.xtrip.base.BaseController;
import com.xtrip.common.BusinessConstant;
import com.xtrip.common.TcConstant;
import com.xtrip.hotel.model.Hotel;
import com.xtrip.hotel.service.HotelService;
import com.xtrip.net.request.TcRequest;
import com.xtrip.net.util.RequestSend;
import com.xtrip.util.XtripLogger;

@Controller("mhotelController")
@RequestMapping("mhotel")
public class MHotelController extends BaseController{

	@Resource(name = "hotelService")
	private HotelService hotelService;
	
	private XtripLogger log = getLogger(MHotelController.class);

	@RequestMapping("/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is mhotelController");
		return "/adm/index";
	}
	@RequestMapping("/addTcHotels")
	public void addTcHotels(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addTcHotels");
		
		TcHotelList thl = new TcHotelList();
		thl.parseAdd("E:\\nexttrips\\17uu\\静态数据文件\\hotellist.xml");
		List<Hotel> hList = thl.gethList();
		int hsuccCnt = 0;
		int hfailCnt = 0;
		int htot = hList.size();
		log.start("导入TC酒店列表");
		for(Hotel h:hList){
			boolean succ = hotelService.insertWithId(h);
			if(succ)hsuccCnt++;
			else {
				log.i("name:"+h.getName()+",tc_hid:"+h.getTcHId()+",upTime:"+h.getTcUpTime());
				hfailCnt++;
			}
		}
		log.i("TOTAL:"+htot+"  SUCCESS:"+hsuccCnt+"  FAIL:"+hfailCnt);
		log.end("导入TC酒店列表");
		JSONObject jo = new JSONObject();
		jo.put("hsuccCnt", hsuccCnt);
		jo.put("hfailCnt", hfailCnt);
		jo.put("htot", htot);
		out(response, jo);
	}
	@RequestMapping("/addTcHotelDetail")
	public void addTcHotelDetail(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addTcHotelDetail");
		
		SimpleQuery sq = new SimpleQuery(Hotel.class);
		List<Hotel> hList = sq.list();
		for(Hotel h : hList){
			String tc_hid = h.getTcHId();
			Map<String,String> params = new HashMap<String,String>();
			params.put("hotelId", "7110");
			params.put("cs", "1");
			String data = TcRequest.generateXml(TcConstant.METHOD_HOTEL_DETAIL, params);
			System.out.println(data);
			String resp = new RequestSend(TcConstant.REQUEST_HOTEL_URL, BusinessConstant.TYPE_HOTEL, BusinessConstant.PROVIDER_HOTEL_17U, data).post();
			System.out.println(resp);
		}
		int hsuccCnt = 0;
		int hfailCnt = 0;
		int htot = hList.size();
		log.start("导入TC酒店列表");
		for(Hotel h:hList){
			boolean succ = hotelService.insertWithId(h);
			if(succ)hsuccCnt++;
			else {
				log.i("name:"+h.getName()+",tc_hid:"+h.getTcHId()+",upTime:"+h.getTcUpTime());
				hfailCnt++;
			}
		}
		log.i("TOTAL:"+htot+"  SUCCESS:"+hsuccCnt+"  FAIL:"+hfailCnt);
		log.end("导入TC酒店列表");
		JSONObject jo = new JSONObject();
		jo.put("hsuccCnt", hsuccCnt);
		jo.put("hfailCnt", hfailCnt);
		jo.put("htot", htot);
		out(response, jo);
	}
}
